﻿using System.Web.Security;
using System.Web.Profile;
using LBSWebService.BusinessLogic.Profile;

/// <summary>
/// Wrapping Profile properties
/// </summary>
public class ProfileEntity
{
    public string OwnAge { get;  set; }
    public string PartnerAge { get; set; }

    public string OwnBuild { get; set; }
    public string PartnerBuild { get; set; }

    public string OwnHighestSchool { get; set; }
    public string PartnerHighestSchool { get; set; }

    public string OwnSex { get; set; }
    public string PartnerSex { get; set; }
}

/// <summary>
/// Worker class for manage userprofiles
/// </summary>
public class ProfileManagement
{
    /// <summary>
    /// String means user doesn't care or doesn't set a value
    /// for a property
    /// </summary>
    const string DONTCARE = "0";

    /// <summary>
    /// Compares 2 ProfileCommon objects and reD:\bme\Önlab\LocationBasedSolution\Server\LBSWebService\BusinessLogic\turns a percent value.
    /// If sex properties does'nt match, returns 0 percent.
    /// </summary>
    /// <param name="pProfile1">First profile</param>
    /// <param name="pProfile2">Second profile</param>
    /// <returns>A value in percent</returns>
    public static int DoCompare(ProfileCommon pProfile1, ProfileCommon pProfile2)
    {
        int matchingProperties = 0;

        #region Gender
        //Sex comparsion

        if (pProfile1.ExpectedSex != pProfile2.OwnSex || pProfile2.ExpectedSex != pProfile1.OwnSex)
        {
            // if users sex expectation  doesn't match, retrun 0 %
            return 0;
        }

        if (pProfile2.ExpectedAge == DONTCARE || pProfile2.ExpectedAge == pProfile1.OwnAge)
        {
            matchingProperties++;
        }
        #endregion

        #region Age
        //Age comparsion

        if (pProfile1.ExpectedAge == DONTCARE || pProfile1.ExpectedAge == pProfile2.OwnAge)
        {
            matchingProperties++;
        }

        if (pProfile2.ExpectedAge == DONTCARE || pProfile2.ExpectedAge == pProfile1.OwnAge)
        {
            matchingProperties++;
        }
        #endregion

        #region Build
        // Build comparsion
        if (pProfile1.ExpectedBuild == DONTCARE || pProfile1.ExpectedBuild == pProfile2.OwnBuild)
        {
            matchingProperties++;
        }

        if (pProfile2.ExpectedBuild == DONTCARE || pProfile2.ExpectedAge == pProfile1.OwnBuild)
        {
            matchingProperties++;
        }
        #endregion

        #region School
        // School comparsion
        if (pProfile1.ExpectedHighestSchool == DONTCARE || pProfile1.ExpectedHighestSchool == pProfile2.OwnHighestSchool)
        {
            matchingProperties++;
        }

        if (pProfile2.ExpectedHighestSchool == DONTCARE || pProfile2.ExpectedHighestSchool == pProfile1.OwnHighestSchool)
        {
            matchingProperties++;
        }
        #endregion

        // Calculating the matcing percent 
        // -1 is for PicturePath property
        return 100 / ((ProfileCommon.Properties.Count - 1) / matchingProperties);
    }

    /// <summary>
    /// Saves the profile for the current user
    /// </summary>
    /// <param name="pProfile"></param>
    public static void SaveProfile(ProfileEntity pProfile)
    {
        if (Membership.GetUser() != null)
        {
            SaveProfile(pProfile, Membership.GetUser().UserName);
        }
    }

    public static void SaveProfile(ProfileEntity pProfile, string pUserName)
    {
        ProfileCommon profileCommon = (ProfileCommon)ProfileBase.Create(pUserName, true);
        // Gender
        profileCommon.OwnSex = pProfile.OwnSex;
        profileCommon.ExpectedSex = pProfile.PartnerSex;
        // Age
        profileCommon.OwnAge = pProfile.OwnAge;
        profileCommon.ExpectedAge = pProfile.PartnerAge;
        // Build
        profileCommon.OwnBuild = pProfile.OwnBuild;
        profileCommon.ExpectedBuild = pProfile.PartnerBuild;
        // Highest school
        profileCommon.OwnHighestSchool = pProfile.OwnHighestSchool;
        profileCommon.ExpectedHighestSchool = pProfile.PartnerHighestSchool;
        
        profileCommon.Save();
    }

}
